<template>
	<xn-form-container
		:title="formData.id ? '编辑' : '新增'"
		:width="1000"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form
			:wrapper-col="{ span: 16 }"
			:label-col="{ span: 8 }"
			autocomplete="off"
			labelAlign="right"
			ref="formRef"
			:model="formData"
			:rules="formRules"
		>
			<a-row>
				<a-col :span="12">
					<a-form-item label="物料编号" name="code">
						<a-input v-model:value="formData.code" placeholder="请输入物料编号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="物料名称" name="name">
						<a-input v-model:value="formData.name" placeholder="请输入物料名称" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="型号规格" name="spec">
						<a-input v-model:value="formData.spec" placeholder="请输入型号规格" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="品牌" name="brand">
						<a-input v-model:value="formData.brand" placeholder="请输入品牌" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="单位" name="unit">
						<a-input v-model:value="formData.unit" placeholder="请输入单位" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="物料单价" name="unitPrice">
						<a-input v-model:value="formData.unitPrice" placeholder="请输入物料单价" allow-clear>
							<template #addonAfter>
								<span>元</span>
							</template>
						</a-input>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="人工单价" name="laborUnitPrice">
						<a-input v-model:value="formData.laborUnitPrice" placeholder="请输入人工单价" allow-clear>
							<template #addonAfter>
								<span>元</span>
							</template>
						</a-input>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="成本单价" name="costUnitPrice">
						<a-input v-model:value="formData.costUnitPrice" placeholder="请输入成本单价" allow-clear>
							<template #addonAfter>
								<span>元</span>
							</template>
						</a-input>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="综合单价" name="synUnitPrice">
						<a-input v-model:value="formData.synUnitPrice" placeholder="请输入综合单价" allow-clear>
							<template #addonAfter>
								<span>元</span>
							</template>
						</a-input>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="备注">
						<a-textarea
							:autosize="{ minRows: 4, maxRows: 4 }"
							v-model:value="formData.remark"
							placeholder="请输入备注"
						/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">提交</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="productForm">
	import { required, rules } from '@/utils/formRules'
	import promaterialApi from '@/api/base_data/promaterialApi'
	// 定义emit事件
	//PROCUREMENT_PARTNERS
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {}
		}
	}

	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		code: [required('请输入物料编号')],
		name: [required('请输入物料名称')],
		spec: [required('请输入规格')],
		brand: [required('请输入品牌')],
		unit: [required('请输入单位')],
		unitPrice: [required('请输入物料单价'), rules.price],
		synUnitPrice: [required('请输入综合单价'), rules.price],
		laborUnitPrice: [required('请输入人工单价'), rules.price],
		costUnitPrice: [required('请输入成本单价'), rules.price]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				promaterialApi
					.addOrEdit(formData.value.id ? 'edit' : 'add', formData.value)
					.then(() => {
						visible.value = false
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped :type="less">
	.childAddButton {
		margin-bottom: 10px;
	}
</style>
